﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BaseDatos;

namespace SCHD
{
    public partial class WFModulosxPerfil : TDV.Controles.Base.WinForm.BaseForm_Tipo03
    {
        public WFModulosxPerfil()
        {
            InitializeComponent();
        }

        private void WFModulosxPerfil_Load(object sender, EventArgs e)
        {
            CargaGridPerfiles();
        }

       

        private void CboPerfiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            CargaGridPerfiles();
        }
        protected void CargaGridPerfiles()
        {
            GrdPerfiles.DataSource = null;
            string SQL = "select codperf,dscperf from chdperfil where flgeli ='0' order by codperf";
            DataSet dsGridPerfil = ConexionBaseDatos.Conexion.CrearDataSet(SQL, CommandType.Text);
            if (dsGridPerfil.Tables[0].Rows.Count > 0)
            {
                GrdPerfiles.DataSource = dsGridPerfil.Tables[0];
            }
        }

        private void TsbFormularios_Click(object sender, EventArgs e)
        {
            WFModulos form = new WFModulos();
            form.Show();
        }

        private void TsbSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GrdPerfiles_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void GrdPerfiles_SelectionChanged(object sender, EventArgs e)
        {
            CargaFormularios(GrdPerfiles.CurrentRow.Cells[0].Value.ToString());
        }
        protected void CargaFormularios(string codigo)
        {
            GrdFormularios.DataSource = null;
            string SQL = ""+
            "SELECT ap.codform,\n" +
            "       (SELECT fr.nomform\n" +
            "          FROM chdformularios fr\n" +
            "         WHERE fr.codform = ap.codform)\n" +
            "          nomform,\n" +
            "       (CASE WHEN ap.tipperf = 'R' THEN 'Registro' ELSE 'Consulta' END)\n" +
            "          acceso\n" +
            "  FROM chdaccesoxperfil ap where AP.FLGELI = '0'\n" +
            "  and codperf = '"+codigo+"'";

            DataSet dsForm = ConexionBaseDatos.Conexion.CrearDataSet(SQL, CommandType.Text);
            if(dsForm.Tables[0].Rows.Count>0)
            {
                GrdFormularios.DataSource = dsForm.Tables[0];
            }
        }

        private void TsbAdicionar_Click(object sender, EventArgs e)
        {
            if (GrdFormularios.Rows.Count > 0)
            {
                string codigo = "";
                int contador = 0;
                foreach (DataGridViewRow Row in GrdFormularios.Rows)
                {
                    contador = contador + 1;
                    if (contador > 1)
                    {
                        codigo = codigo + ",'" + Row.Cells[0].Value.ToString() + "'";
                    }
                    else
                    {
                        codigo = codigo + "'" + Row.Cells[0].Value.ToString() + "'";
                    }
                }

                WFListFormularios list = new WFListFormularios(codigo);
                list.ShowDialog();
                if (list.DialogResult == DialogResult.OK)
                {
                    try
                    {
                        string codperf = GrdPerfiles.CurrentRow.Cells[0].Value.ToString();
                        string codform = list.CodigoFormulario;
                        string acceso = list.Tipo ? "R" : "C";
                        string user = Environment.UserName.ToUpper();
                        string wks = Environment.MachineName.ToUpper();
                        string SQL = "" +
                            "insert into chdaccesoxperfil (codperf,codform,tipperf,usrcre,fchcre,wkscre,usrmod,fchmod,wksmod,flgeli) " +
                            "                        values('" + codperf + "','" + codform + "','" + acceso + "','" + user + "',sysdate,'" + wks + "','" + user + "',sysdate,'" + wks + "','0')";
                        ConexionBaseDatos.Conexion.EjecutarComando(SQL, CommandType.Text);
                        CargaFormularios(codperf);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            else
            {
                WFListFormularios list = new WFListFormularios("");
                list.ShowDialog();
                if (list.DialogResult == DialogResult.OK)
                {
                    string codperf = GrdPerfiles.CurrentRow.Cells[0].Value.ToString();
                    string codform = list.CodigoFormulario;
                    string acceso = list.Tipo ? "R" : "C";
                    string user = Environment.UserName.ToUpper();
                    string wks = Environment.MachineName.ToUpper();
                    string SQL = "" +
                        "insert into chdaccesoxperfil (codperf,codform,tipperf,usrcre,fchcre,wkscre,usrmod,fchmod,wksmod,flgeli) " +
                        "                        values('" + codperf + "','" + codform + "','" + acceso + "','" + user + "',sysdate,'" + wks + "','" + user + "',sysdate,'" + wks + "','0')";
                    ConexionBaseDatos.Conexion.EjecutarComando(SQL, CommandType.Text);
                    CargaFormularios(codperf);
                }
            }

        }

        private void TsbEliminar_Click(object sender, EventArgs e)
        {
            try
            {
            string codperf = GrdPerfiles.CurrentRow.Cells[0].Value.ToString();
            string codform = GrdFormularios.CurrentRow.Cells[0].Value.ToString();
            ConexionBaseDatos.Conexion.EjecutarComando("delete chdaccesoxperfil where codperf = '" + codperf + "' and codform = '" + codform + "'",CommandType.Text);
            CargaFormularios(codperf);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void TsbPerfiles_Click(object sender, EventArgs e)
        {
            WFPerfiles perfil = new WFPerfiles();
            perfil.ShowDialog();
            if (perfil.DialogResult == DialogResult.OK)
            {
                CargaGridPerfiles();
            }
        }
    }
}
